package ui;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;

import logic.DriveLoader;
import logic.PersonTableCellRenderer;
import logic.PersonTableModel;

public class PanelLoots extends JPanel
{
	private PersonTableModel dkpData = new PersonTableModel();
	private JTable tblDkp = null;
	public static JPanel me = null;

	/**
	 * 
	 */
	private static final long	serialVersionUID	= 1L;

	public PanelLoots()
	{
		me = this;
		initialize();
	}

	private void initialize()
	{		
		//Button SaveRaid
		JButton btnGrant = new JButton("SaveRaid & Pre-Grant Dkp");
		btnGrant.setToolTipText("This action will retrieve the list of players in your raid. You MUST have typed /SaveRaid in Allods Chat BEFORE clicking me. It will then pre-grant 6 Dkp. U can click on me many times, i will not grant two times dkp to the same player. Data will NOT be saved to google doc yet.");
		btnGrant.addActionListener(new ActionListener()
		{
			
			@Override
			public void actionPerformed(ActionEvent arg0)
			{
				// Lets get the list of people in the raid, make sure they already exist, if not add new entry, and grant 6 dkp to all if not already done.
				boolean granted = dkpData.grantDkp();
				if (granted)
					JOptionPane.showMessageDialog(me,"I have found these players in raid: " + dkpData.getRaidPlayers());
				else
					JOptionPane.showMessageDialog(me,"You have not typed /SaveRaid in Allods chat. What are you trying to do?!");
			}
		});
		
		
		//Button WhoCanLoot?
		JButton btnWhoCanLoot = new JButton("Who can loot?");
		btnWhoCanLoot.setToolTipText("This action will retrieve the list of players that plussed in raid chat and enable you to find highest dkp.");
		btnWhoCanLoot.addActionListener(new ActionListener()
		{
			
			@Override
			public void actionPerformed(ActionEvent arg0)
			{
				String res = dkpData.WhoCanLoot();
				if (res.length() > 0)
					JOptionPane.showMessageDialog(me, res);
				else
					JOptionPane.showMessageDialog(me, "Nobody Plused! It was still a good idea to press on me I may or not have loaded the last drops.");
			}
		});
		
		//Button SaveToGoogleDoc?
		JButton btnSaveToGoogleDoc = new JButton("SaveToGoogleDoc");
		btnSaveToGoogleDoc.setToolTipText("Do NOT Forget to click on me! Otherwise nothing will be saved! I could remind you and force u to click me but i m nice so i leave it up to you!");
		btnSaveToGoogleDoc.addActionListener(new ActionListener()
		{
			
			@Override
			public void actionPerformed(ActionEvent arg0)
			{
				int popup_res = JOptionPane.showConfirmDialog(me, "You will create a new Raid on the Google Doc. Be patient this could take a while.",
						"Confirmation", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
				if (popup_res == JOptionPane.OK_OPTION)
				{
					// Lets make sure We have all last loots.
					dkpData.WhoCanLoot();
					// Here we go to Save collected data on google doc. TODO: use a batch update instead of cell by cell...
					Boolean saved = DriveLoader.getInstance().updateSpreadsheetData(dkpData.getData());
					if (!saved)
						JOptionPane.showMessageDialog(me, "An error occured during saving. Maybe u dont have the rights to edit the google doc. Probably lost google connection from the moment u connected till now. Sad news all is lost.");
					else
						JOptionPane.showMessageDialog(me, "Gloire aux Flibus! Long life to Ascendancy!");
				}
				//TODO: decide what! i guess closing app?
			}
		});
		
		// Table dkp, Gets Data from Google Doc.
		tblDkp = new JTable(dkpData);
		PersonTableCellRenderer renderer = new PersonTableCellRenderer();
		tblDkp.setDefaultRenderer(Object.class, renderer);
		tblDkp.setDefaultRenderer(Long.class, renderer);
		tblDkp.setRowSorter(new TableRowSorter<TableModel>(tblDkp.getModel()));
		JScrollPane scrollPaneTblDkp = new JScrollPane(tblDkp);
		dkpData.addPersonSet(DriveLoader.getInstance().getSpreadsheetData("DkpLoots"));
		tblDkp.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		tblDkp.getColumnModel().getColumn(0).setPreferredWidth(120);
		tblDkp.getColumnModel().getColumn(1).setPreferredWidth(60);
		tblDkp.getColumnModel().getColumn(2).setPreferredWidth(60);
		tblDkp.getColumnModel().getColumn(3).setPreferredWidth(420);
		
		// add in nice order
		setLayout(new BorderLayout());
		add(btnGrant, BorderLayout.NORTH);
		add(btnWhoCanLoot, BorderLayout.WEST);
		add(scrollPaneTblDkp, BorderLayout.CENTER);
		add(btnSaveToGoogleDoc, BorderLayout.SOUTH);
		
	}

}
